﻿using System.Collections.Generic;
using DomainModel;
using NHibernate;
using Repository.Abstract;

namespace Repository.NHibernate
{
    public abstract class Repository<T> : IRepository<T> where T: EntityBase
    {
        protected ISession Session;

        protected Repository()
        {
            Session = NHibernateHelper.GetSession();
        }

        public virtual void SaveOrUpdate(T entity)
        {
            using (Session)
            {
                Session.SaveOrUpdate(entity);
            }
        }

        public virtual T GetById(long id)
        {
            using (Session)
            {
                return Session.Get<T>(id);
            }
        }

        public virtual IEnumerable<T> GetAll()
        {
            using (Session)
            {
                return Session.QueryOver<T>().List<T>();
            }
        }

        public virtual void Delete(T entity)
        {
            using (Session)
            {
                Session.Delete(entity);
            }
        }
    }
}
